using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The StudentFamilyMemberRelationshipTableDB class is responsible for interacting with the database to retrieve and store information
/// about StudentFamilyMemberRelationshipTable objects.
/// </summary>
public class StudentFamilyMemberRelationshipTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of StudentFamilyMemberRelationshipTable from the underlying datasource.
/// </summary>
/// <param name="familyMemberAccountKey">The unique FamilyMemberAccountKey of the StudentFamilyMemberRelationshipTable in the database.</param>
/// <returns>An StudentFamilyMemberRelationshipTable when the FamilyMemberAccountKey was found in the database, or null otherwise.</returns>
public static StudentFamilyMemberRelationshipTable GetItem(int familyMemberAccountKey)
{
StudentFamilyMemberRelationshipTable myStudentFamilyMemberRelationshipTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStudentFamilyMemberRelationshipTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@familyMemberAccountKey", familyMemberAccountKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myStudentFamilyMemberRelationshipTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myStudentFamilyMemberRelationshipTable;
}
}

/// <summary>
/// Returns a list with StudentFamilyMemberRelationshipTable objects.
/// </summary>
/// <returns>A generics List with the StudentFamilyMemberRelationshipTable objects.</returns>
public static StudentFamilyMemberRelationshipTableList GetList()
{
StudentFamilyMemberRelationshipTableList tempList = new StudentFamilyMemberRelationshipTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStudentFamilyMemberRelationshipTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with StudentFamilyMemberRelationshipTable objects.
/// </summary>
/// <returns>A generics List with the StudentFamilyMemberRelationshipTable objects.</returns>
public static StudentFamilyMemberRelationshipTableList GetListByRelationshipTypeCode(short RelationshipTypeCode)
{
StudentFamilyMemberRelationshipTableList tempList = new StudentFamilyMemberRelationshipTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStudentFamilyMemberRelationshipTableSelectListByRelationshipTypeCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@relationshipTypeCode", RelationshipTypeCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a StudentFamilyMemberRelationshipTable in the database.
/// </summary>
/// <param name="myStudentFamilyMemberRelationshipTable">The StudentFamilyMemberRelationshipTable instance to save.</param>
/// <returns>The new FamilyMemberAccountKey if the StudentFamilyMemberRelationshipTable is new in the database or the existing FamilyMemberAccountKey when an item was updated.</returns>
public static int Save(StudentFamilyMemberRelationshipTable myStudentFamilyMemberRelationshipTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStudentFamilyMemberRelationshipTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myStudentFamilyMemberRelationshipTable.FamilyMemberAccountKey == -1)
{myCommand.Parameters.AddWithValue("@familyMemberAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@familyMemberAccountKey", myStudentFamilyMemberRelationshipTable.FamilyMemberAccountKey);
}
if (myStudentFamilyMemberRelationshipTable.RelationshipTypeCode == null){
myCommand.Parameters.AddWithValue("@relationshipTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@relationshipTypeCode", myStudentFamilyMemberRelationshipTable.RelationshipTypeCode);
}
if (string.IsNullOrEmpty(myStudentFamilyMemberRelationshipTable.EmergencyContact))
{
myCommand.Parameters.AddWithValue("@emergencyContact", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@emergencyContact", myStudentFamilyMemberRelationshipTable.EmergencyContact);
}
if (string.IsNullOrEmpty(myStudentFamilyMemberRelationshipTable.Remark))
{
myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remark", myStudentFamilyMemberRelationshipTable.Remark);
}
if (myStudentFamilyMemberRelationshipTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myStudentFamilyMemberRelationshipTable.Status);
}
if (myStudentFamilyMemberRelationshipTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myStudentFamilyMemberRelationshipTable.CreatedDateTime);
}
if (myStudentFamilyMemberRelationshipTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myStudentFamilyMemberRelationshipTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a StudentFamilyMemberRelationshipTable from the database.
/// </summary>
/// <param name="familyMemberAccountKey">The FamilyMemberAccountKey of the StudentFamilyMemberRelationshipTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int familyMemberAccountKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocStudentFamilyMemberRelationshipTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@familyMemberAccountKey", familyMemberAccountKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the StudentFamilyMemberRelationshipTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static StudentFamilyMemberRelationshipTable FillDataRecord(IDataRecord myDataRecord )
{
StudentFamilyMemberRelationshipTable myStudentFamilyMemberRelationshipTable = new StudentFamilyMemberRelationshipTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("StudentAccountKey")))
{
myStudentFamilyMemberRelationshipTable.StudentAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("StudentAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FamilyMemberAccountKey")))
{
myStudentFamilyMemberRelationshipTable.FamilyMemberAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("FamilyMemberAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RelationshipTypeCode")))
{
myStudentFamilyMemberRelationshipTable.RelationshipTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("RelationshipTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmergencyContact")))
{
myStudentFamilyMemberRelationshipTable.EmergencyContact = myDataRecord.GetString(myDataRecord.GetOrdinal("EmergencyContact"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
{
myStudentFamilyMemberRelationshipTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myStudentFamilyMemberRelationshipTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myStudentFamilyMemberRelationshipTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myStudentFamilyMemberRelationshipTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myStudentFamilyMemberRelationshipTable;
}
}

 } 
